<?php

namespace app\admin\controller\wechat;

use app\admin\controller\Base;
use think\Db;

/**
 * 用户组管理
 * Class GroupController
 * @package app\admin\controller\auth
 */
class tags extends Base
{

    /**
     * 主页面
     * @return mixed
     */
    public function index()
    {
        return view();
    }

    public function lists()
    {
        $map = array();
        $data = array();
        $post = $this->request->param();
        unset($post['page']);
        unset($post['limit']);
        $data['count'] = Db::name('wx_user_tag')
            ->where($post)
            ->count();

        $data['data'] = Db::name('wx_user_tag')
            ->where($post)
            // ->fetchSql(true)
            ->order('id desc')->page(input("page"), input('limit'))->select();

        if ($data['count'] > 0 && $data['data']) {
            $data['msg'] = '获取成功';
        } else {
            $data['msg'] = '获取数据为空';
        }
        $data['code'] = 0;
        return json($data);
    }


    public function add()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();
            if (!isset($post['name']) || empty($post['name'])) {
                $this->error(' 标签不能为空');
            }
            $url = "https://api.weixin.qq.com/cgi-bin/tags/create?access_token=" . cache('access_token');
            $data['tag']['name'] = $post['name'];
            $status = $this->seturl($url, $data);
            if ($status['status'] == 1) {
                $post['tag_id'] = $status['id'];
                if (Db::name('wx_user_tag')->insert($post) !== false) {
                    $this->success('操作成功');
                }
            }
            $this->error('添加失败' . $status['msg']);

        } else {
            $list = DB::name('auth_group')->field('id,title')->select();
            $this->assign('group_list', $list);
            return view();
        }
    }

    public function seturl($url, $data)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data, JSON_UNESCAPED_UNICODE));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $tmpInfo = curl_exec($ch);
        if (curl_errno($ch)) {
            // dump(curl_error($ch));
        }
        curl_close($ch);
        $res = json_decode($tmpInfo, true);
        if (!empty($res['tag']['id']) && $res['tag']['id'] != 0) {
            $data['status'] = 1;
            $data['id'] = $res['tag']['id'];
            return $data;
        } else {
            //dump($res);
            $data['status'] = 0;
            $data['msg'] = '未知错误';
            if (!empty($res['errcode']) || $res['errcode'] == 0) {
                switch ($res['errcode']) {
                    case 0:
                        $data['status'] = 1;
                        $data['msg'] = '正常';
                    case -1:
                        $data['msg'] = '系统繁忙';
                        break;
                    case 45157:
                        $data['msg'] = '标签名非法，请注意不能和其他标签重名';
                        break;
                    case 45158:
                        $data['msg'] = '标签名长度超过30个字节';
                        break;
                    case 45058:
                        $data['msg'] = '不能修改0/1/2这三个系统默认保留的标签';
                        break;
                    case 45057:
                        $data['msg'] = '该标签下粉丝数超过10w，不允许直接删除';
                        break;
                    case 40003:
                        $data['msg'] = '传入非法的openid';
                        break;
                    case 45159:
                        $data['msg'] = '非法的tag_id';
                        break;
                    case 40032:
                        $data['msg'] = '每次传入的openid列表个数不能超过50个';
                        break;
                    case 45059:
                        $data['msg'] = '有粉丝身上的标签数已经超过限制，即超过20个';
                        break;
                    case 49003:
                        $data['msg'] = '传入的openid不属于此AppID';
                        break;
                    default:
                        $data['msg'] = $res['errcode'];
                }
            }
            return $data;
        }
    }


    public function delete()
    {
        if ($this->request->isPost()) {
            $post = $this->request->param();

            if (!isset($post['id'])) {
                $this->error('请求参数错误');
            }
            $url = 'https://api.weixin.qq.com/cgi-bin/tags/delete?access_token=' . cache('access_token');
            $data['tag']['id'] = DB::name('wx_user_tag')->where('id', $post['id'])->value('tag_id');
            $status = $this->seturl($url, $data);
            if ($status['status'] == 1) {
                if (Db::name('wx_user_tag')->delete($post) !== false) {
                    $this->success('操作成功');
                }
            }
            $this->error('删除失败' . $status['msg']);
        }
    }


    public function cusertag()
    {
        $post = $this->request->param();

        if ($this->request->isPost()) {

            if (!isset($post['tag_id'])) {
                $this->error('请求参数错误');
            }
            $tag_id = DB::name('wx_user_tag')->where('id', $post['tag_id'])->value('tag_id');
            $url = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchuntagging?access_token=' . cache('access_token');
            $list = DB::view('admin', 'openid')
                ->view('auth_group_access', 'group_id,uid', 'admin.id=auth_group_access.uid')
                ->where('group_id', $post['group_id'])
                ->where('openid', '<>', '')
                ->select();
            $openid = array_column($list, 'openid');
            //dump($openid);
            $data['openid_list'] = $openid;
            $data['tagid'] = $tag_id;
            $this->seturl($url, $data);
            $url = 'https://api.weixin.qq.com/cgi-bin/tags/members/batchtagging?access_token=' . cache('access_token');
            $status = $this->seturl($url, $data);
            if ($status['status'] == 1) {
                // $post['tag_id'] = $status['id'];
                $this->success('操作成功');
            }
            $this->error('编辑失败' . $status['msg']);

        } else {
            $list = DB::name('auth_group')->field('id,title')->select();
            $this->assign('group_list', $list);
            $this->assign('tag_id', $post['id']);
            return view();

        }
    }

    public function edit()
    {
        if ($this->request->isPost()) {

            $post = $this->request->param();
            if (!isset($post['name']) || empty($post['name'])) {
                $this->error(' 标签不能为空');
            }
            $data['tag']['id'] = DB::name('wx_user_tag')->where('id', $post['id'])->value('tag_id');
            $data['tag']['name'] = $post['name'];
            $url = 'https://api.weixin.qq.com/cgi-bin/tags/update?access_token=' . cache('access_token');
            // dump($url);
            $status = $this->seturl($url, $data);
            if ($status['status'] == 1) {
                // $post['tag_id'] = $status['id'];
                if (Db::name('wx_user_tag')->update($post) !== false) {
                    $this->success('操作成功');
                }
            }
            $this->error('编辑失败' . $status['msg']);

        }

    }


}